#include "smartwindow.h"
#include <stdio.h>
#include "globalvars.h"

#include "ohos_init.h"
#include "cmsis_os2.h"

int g_window_status = 0;
int g_fire_alarm_status = 0;
int g_fire_alarm = 0;

// int g_window_control_flag = 1;
// int g_window_work_mode = 2;
// int g_humi_flag = 1;
// int g_temp_flag = 1;
// int g_gas_flag = 0;

void Smartwindow_task(void)
{
    Window_init();
    printf("The window is activated successfully!!\r\n");
    // usleep(100 * 1000);
    while (1)
    {
        if(g_gas_flag == 1 && g_temp_flag == 1)
        {
            g_fire_alarm = 1;
        }
        else
        {
            g_fire_alarm = 0;
        }
        if(g_window_work_mode == 1)
        //模式1，用来烟雾浓度高，窗户开。浓度低的话，窗户关
        {
            if(g_window_control_flag == 1 && g_window_status == 0)
            {
                Window_Switch(1);  //开
                usleep(3 * 1000 * 1000);
                Window_Switch(0); // 停
                g_window_status = 1;
                g_window_control_flag = 0;
            }
            if(g_window_control_flag == 2 && g_window_status == 1)
            {
                // usleep(10 * 1000 * 1000);
                Window_Switch(2); // 关
                usleep(3 * 1000 *1000);
                Window_Switch(0); // 停
                g_window_status = 0;
                g_window_control_flag = 0;
            }
        }
        //模式2，用于远程控制
        if(g_window_work_mode == 2)
        {
            if(g_window_status == 1 && g_humi_flag == 1)
            {
                Window_Switch(2);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 0;
            }
            if(g_window_status == 0 && g_fire_alarm == 0 && g_humi_flag == 0 )
            {
                Window_Switch(1);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 1;
            }
            if(g_window_status == 1 && g_fire_alarm == 1)
            {
                Window_Switch(2);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 0;
                // fire_alarm = 1;
            }
            // if(g_fire_alarm == 1)
            // {
            //     Beep_switch(1);
            //     g_fire_alarm_status = 1;
            // }
            // if(g_fire_alarm == 0)
            // {
            //     Beep_switch(0);
            //     g_fire_alarm_status = 0;
            // }
        }
        if(g_window_work_mode == 3)
        {
            //如果状态关，烟雾高，控制为停止
            if(g_window_status == 0 && g_window_control_flag == 1 && g_humi_flag == 0 && g_fire_alarm == 0)
            { 
                Window_Switch(1);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 1;
                // g_window_control_flag = 0;
                printf("mode = 3->1 ");
            }
            //如果状态开，烟雾低，控制为停止
            if(g_window_status == 1 && g_window_control_flag == 2)
            {
                Window_Switch(2); // 关
                usleep(3 * 1000 *1000);
                Window_Switch(0); // 停
                // g_window_control_flag = 0;
                g_window_status = 0;
                // Windows_status = 0;   
                printf("mode = 3->2 ");
            }
            if(g_window_status == 1 && g_humi_flag == 1) 
            //状态开，浓度低，控制关，
            {
                Window_Switch(2);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 0;
                printf("mode = 3->4 ");
            }
            if(g_window_status == 1 && g_fire_alarm == 1)
            {
                Window_Switch(2);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 0;
            }
            if(g_window_status == 0 && g_humi_flag == 0 && g_fire_alarm == 0 && g_window_control_flag == 0)
            {
                Window_Switch(1);
                usleep(3 * 1000 * 1000);
                Window_Switch(0);
                g_window_status = 1;
            }
            // if(g_fire_alarm == 1)
            // {
            //     Beep_switch(1);
            //     // g_fire_alarm_status = 1;
            // }
            // if(g_fire_alarm == 0)
            // {
            //     Beep_switch(0);
            //     // g_fire_alarm_status = 0;
            // }
        }
        usleep(3 * 1000 * 1000);
    }
    
}

void Smartwindow_Entry(void)
{
	usleep(3 * 1000 * 1000); //延时3s
	osThreadAttr_t attr; //任务属性结构体
	attr.name = "Smart_task"; //任务名称
	attr.stack_size = 4096; //任务内存大小
	attr.priority = 25;
    if(osThreadNew((osThreadFunc_t)Smartwindow_task, NULL, &attr) == NULL)
    {
        printf("[Smartwindow] Falied to create Smartwindow_Task!\n");
    }
    printf("[Smartwindow] Succeed to create Smartwindow_Task!\n");
}

SYS_RUN(Smartwindow_Entry);